package com.jsu.hotelmanagement.service.serviceImpl;

import com.jsu.hotelmanagement.dao.UserDao;
import com.jsu.hotelmanagement.pojo.vo.CommonResult;
import com.jsu.hotelmanagement.pojo.vo.LoginResponseVO;
import com.jsu.hotelmanagement.pojo.vo.LoginVO;
import com.jsu.hotelmanagement.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jsu.hotelmanagement.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * @author Mo
 * @createTime 2022/12/4 19:09
 * @description 用户接口实现类
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserDao userDao;

    @Override
    public CommonResult login(LoginVO loginVO, HttpServletRequest request) {
        try {
            int i = userDao.queryAccount(loginVO.getUserName());
            if (i <= 0) {
                return new CommonResult(444, "该账号未注册，请注册");
            }
            LoginResponseVO login = userDao.queryAccountInfo(loginVO);
            if (login != null)   {
                HashMap<String, String> payload = new HashMap<>();
                payload.put("userId", login.getUserId().toString());
                String token = JWTUtil.getTOKEN(payload);
                login.setToken(token);
                System.out.println(token);
                return new CommonResult(200, "登陆成功", login);
            }
            return new CommonResult(444, "用户名或密码错误");
        } catch (Exception e) {
            return new CommonResult(444, "异常");
        }
    }
}
